package com.itheima.health.service.impl;

import com.itheima.health.dao.PermissionDao;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.dao.UserDao;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhangmeng
 * @description 用户服务实现类
 * @date 2019/9/6
 **/
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    @Override
    public User findByUsername(String username) {
        User user = userDao.selectByUsername(username);
        if (user!=null){
            //根据用户id查询角色表
            List<Role> roles=roleDao.selectByUserId(user.getId());
            //根据角色的id查询权限表
            for (Role role : roles) {
                List<Permission> permissionList=permissionDao.selectByRoleId(role.getId());
                role.getPermissions().addAll(permissionList);
            }
            user.getRoles().addAll(roles);
        }
        //调用DAO查询数据
        return user;
    }
}
